package com.king.java.tools;

/**
 * 描述: 代码拉取工具
 * 从远程主机拉取代码到机器人上
 * @author : sujinchen
 * @date : 2020/10/22
 */
public class GitRemotePull {
    public static void main(String[] args) {
        GitRemotePull git = new GitRemotePull();
        System.out.println(git.update());
    }
    private String dirName = "KavaBot";
    //本机拉取的代码仓库，直接复制即可，注意开头和结尾不要有空格
   private String line = "base_locomotion  kava_application  kava_communication  lidar_slam  mission_manager  navigation  robot_toolkits  sensors";
   private String[] gits = line.split("  ");

    /**
     * 从远程分支拉取代码
     * @return
     */
    public String init() {
        StringBuilder builder = new StringBuilder();
        //头部可手动创建
        builder.append("#!/bin/bash\n");
        builder.append("cd ~\n");
        builder.append("mkdir "+dirName+"\n");
        builder.append("cd "+dirName+"\n");
        builder.append("mkdir src\n");
        builder.append("cd src\n");
        builder.append("#start\n");



        //核心部分，谨慎修改
        for (String git :
                gits) {
            builder.append("mkdir "+git+"\n");
            builder.append("cd "+git+"\n");
            builder.append("git init\n");
            builder.append("git remote add origin ssh://cvte@$1/home/cvte/KavaBot/src/"+git+"\n");
            builder.append("git pull\n");
            builder.append("git checkout test\n");
            builder.append("cd ..\n");
            builder.append("\n");
        }
        builder.append("#end\n");

        //回到父级目录
        builder.append("cd ~/"+dirName+"\n");

        System.out.println(builder.toString());
        return builder.toString();
    }

    public String update() {
        StringBuilder builder = new StringBuilder();
        builder.append("#!/bin/bash\n");
        builder.append("cd ~\n");
        String dirName = "KavaBot";
        builder.append("cd ~/"+dirName+"\n");
        builder.append("cd src\n");
        //核心部分，谨慎修改
        for (String git :
                gits) {
            builder.append("echo \">>>>>>>>>>>"+git+"<<<<<<<<<<<<\"\n");
            builder.append("cd "+git+"\n");
            builder.append("git pull\n");
            builder.append("git checkout dev\n");
            builder.append("git pull\n");
            builder.append("cd ..\n");
            builder.append("\n");
        }
        builder.append("cd ~/"+dirName+"\n");

        return builder.toString();

    }

}
